package org.jeecg.modules.demo.sermanager.controller;

import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.demo.sermanager.entity.*;
import org.jeecg.modules.demo.sermanager.mapper.NetptJobIplistMapper;
import org.jeecg.modules.demo.sermanager.mapper.NetptJobSerMapper;
import org.jeecg.modules.demo.sermanager.service.*;
import org.jeecg.modules.demo.sermanager.vo.NetptJobReq;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.vo.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.sermanager.vo.NetptJobPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: netpt_job
 * @Author: jeecg-boot
 * @Date:   2022-01-07
 * @Version: V1.0
 */
@Api(tags="任务管理")
@RestController
@RequestMapping("/sermanager/netptJob")
@Slf4j
public class NetptJobController{
	@Autowired
	private INetptJobService netptJobService;
	@Autowired
	private INetptJobSerService netptJobSerService;
	@Autowired
	private INetptJobDetectionService netptJobDetectionService;
	@Autowired
	private INetptJobIplistService netptJobIplistService;
	@Autowired
	private INetptJobRetService netptJobRetService;
	@Autowired
	private NetptJobIplistMapper netptJobIplistMapper;
	 @Autowired
	 private NetptJobSerMapper netptJobSerMapper;
	
	/**
	 * 分页列表查询
	 *
	 * @param netptJob
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "netpt_job-分页列表查询")
	@ApiOperation(value="任务分页列表", notes="任务分页列表")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(NetptJob netptJob,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<NetptJob> queryWrapper = QueryGenerator.initQueryWrapper(netptJob, req.getParameterMap());
		Page<NetptJob> page = new Page<NetptJob>(pageNo, pageSize);
		IPage<NetptJob> pageList = netptJobService.findNetptJobList(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param NetptJobReq
	 * @return
	 */
	@AutoLog(value = "netpt_job-添加")
	@ApiOperation(value="任务添加", notes="任务添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody NetptJobReq NetptJobReq) {
		NetptJob netptJob = new NetptJob();
		BeanUtils.copyProperties(NetptJobReq, netptJob);
		netptJobService.saveMain(netptJob, NetptJobReq.getNetptJobSer(),NetptJobReq.getNetptJobDetectionList());
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param netptJobReq
	 * @return
	 */
	@AutoLog(value = "netpt_job-编辑")
	@ApiOperation(value="任务编辑", notes="任务编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody NetptJobReq netptJobReq) {
		NetptJob netptJob = new NetptJob();
		BeanUtils.copyProperties(netptJobReq, netptJob);
		NetptJob netptJobEntity = netptJobService.getById(netptJob.getId());
		if(netptJobEntity==null) {
			return Result.error("未找到对应数据");
		}
		netptJobService.updateMain(netptJob, netptJobReq.getNetptJobSer(),netptJobReq.getNetptJobDetectionList());
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "netpt_job-通过id删除")
	@ApiOperation(value="任务-通过id删除", notes="任务-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		netptJobService.delMain(id);
		return Result.OK("删除成功!");
	}

	 /**
	  *   通过任务id暂停
	  *
	  * @return
	  */
	 @AutoLog(value = "暂停任务")
	 @ApiOperation(value="暂停任务", notes="暂停任务")
	 @PutMapping(value = "/stop")
	 public Result<?> stop(@RequestBody NetptJobReq NetptJobReq) {

		 NetptJob netptJob=netptJobService.getById(NetptJobReq.getId());
		 netptJob.setState(-1);
		 netptJobService.updateById(netptJob);
		 //iplist
		 QueryWrapper<NetptJobIplist> queryWrapper=new QueryWrapper();
		 queryWrapper.eq("job_id",NetptJobReq.getId());
		 NetptJobIplist entity=new NetptJobIplist();
		 entity.setState(-1);
		 netptJobIplistMapper.update(entity,queryWrapper);

		 //serve
		 QueryWrapper<NetptJobSer> querySerWrapper=new QueryWrapper();
		 querySerWrapper.eq("job_id",NetptJobReq.getId());
		 NetptJobSer entitySer=new NetptJobSer();
		 entitySer.setState(-1);
		 netptJobSerMapper.update(entitySer,querySerWrapper);

		 return Result.OK("暂停成功!");
	 }


	 /**
	  *   通过任务id暂停
	  *
	  * @return
	  */
	 @AutoLog(value = "继续任务")
	 @ApiOperation(value="继续任务", notes="继续任务")
	 @PutMapping(value = "/execute")
	 public Result<?> execute(@RequestBody NetptJobReq NetptJobReq) {

		 NetptJob netptJob=netptJobService.getById(NetptJobReq.getId());
		 netptJob.setState(1);
		 netptJobService.updateById(netptJob);
		 //iplist
		 QueryWrapper<NetptJobIplist> queryWrapper=new QueryWrapper();
		 queryWrapper.eq("job_id",NetptJobReq.getId());
		 NetptJobIplist entity=new NetptJobIplist();
		 entity.setState(1);
		 netptJobIplistMapper.update(entity,queryWrapper);

		 //serve
		 QueryWrapper<NetptJobSer> querySerWrapper=new QueryWrapper();
		 querySerWrapper.eq("job_id",NetptJobReq.getId());
		 NetptJobSer entitySer=new NetptJobSer();
		 entitySer.setState(1);
		 netptJobSerMapper.update(entitySer,querySerWrapper);

		 return Result.OK("执行成功!");
	 }




	 /**
	  *   通过任务id暂停
	  *
	  * @return
	  */
	 @AutoLog(value = "停止任务")
	 @ApiOperation(value="停止任务", notes="停止任务")
	 @PutMapping(value = "/pause")
	 public Result<?> pause(@RequestBody NetptJobReq NetptJobReq) {

		 NetptJob netptJob=netptJobService.getById(NetptJobReq.getId());
		 netptJob.setState(-2);
		 netptJobService.updateById(netptJob);
		 //iplist
		 QueryWrapper<NetptJobIplist> queryWrapper=new QueryWrapper();
		 queryWrapper.eq("job_id",NetptJobReq.getId());
		 NetptJobIplist entity=new NetptJobIplist();
		 entity.setState(-2);
		 netptJobIplistMapper.update(entity,queryWrapper);

		 //serve
		 QueryWrapper<NetptJobSer> querySerWrapper=new QueryWrapper();
		 querySerWrapper.eq("job_id",NetptJobReq.getId());
		 NetptJobSer entitySer=new NetptJobSer();
		 entitySer.setState(-2);
		 netptJobSerMapper.update(entitySer,querySerWrapper);

		 return Result.OK("停止成功!");
	 }

	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "netpt_job-批量删除")
	//@ApiOperation(value="netpt_job-批量删除", notes="netpt_job-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.netptJobService.delBatchMain(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "netpt_job-通过id查询")
	@ApiOperation(value="netpt_job-通过id查询", notes="netpt_job-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		NetptJob netptJob = netptJobService.getById(id);
		if(netptJob==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(netptJob);

	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "netpt_job_ser通过主表ID查询")
	@ApiOperation(value="通过任务id查询任务检测服务列表", notes="通过任务id查询任务检测服务列表")
	@GetMapping(value = "/queryNetptJobSerByMainId")
	public Result<?> queryNetptJobSerListByMainId(@RequestParam(name="id",required=true) String id) {
		List<NetptJobSer> netptJobSerList = netptJobSerService.selectByMainId(id);
		return Result.OK(netptJobSerList);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "netpt_job_detection通过主表ID查询")
	@ApiOperation(value="通过任务id查询任务检测类型", notes="通过任务id查询任务检测类型")
	@GetMapping(value = "/queryNetptJobDetectionByMainId")
	public Result<?> queryNetptJobDetectionListByMainId(@RequestParam(name="id",required=true) String id) {
		List<NetptJobDetection> netptJobDetectionList = netptJobDetectionService.selectByMainId(id);
		return Result.OK(netptJobDetectionList);
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "netpt_job_iplist通过主表ID查询")
	@ApiOperation(value="通过任务id 查询任务iplist", notes="通过任务id 查询任务iplist")
	@GetMapping(value = "/queryNetptJobIplistByMainId")
	public Result<?> queryNetptJobIplistListByMainId(@RequestParam(name="id",required=true) String id) {
		NetptJob netptJob=netptJobService.getById(id);
		if(netptJob.getDetectionTarget().equals("1")){
			List<NetptJobIplist> netptJobIplistList = netptJobIplistService.selectByMainId(id);
			return Result.OK(netptJobIplistList);
		}else{
			List<NetptJobSer> netptJobSerList = netptJobSerService.selectByMainId(id);
			return Result.OK(netptJobSerList);
		}
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "netpt_job_ret通过主表ID查询")
	@ApiOperation(value="通过任务主表id 查询任务结果列表", notes="通过任务主表id 查询任务结果列表")
	@GetMapping(value = "/queryNetptJobRetByMainId")
	public Result<?> queryNetptJobRetListByMainId(@RequestParam(name="id",required=true) String id) {
		List<NetptJobRet> netptJobRetList = netptJobRetService.selectByMainId(id);
		return Result.OK(netptJobRetList);
	}



	 @AutoLog(value = "检测结果列表")
	 @ApiOperation(value="检测结果列表", notes="检测结果列表")
	 @GetMapping(value = "/queryNetptJobRet")
	 public Result<?> queryNetptJobRet(NetptJobRet netptJobRet,
									   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									   HttpServletRequest req) {
		 QueryGenerator.initQueryWrapper(netptJobRet,req.getParameterMap());
		 QueryWrapper<NetptJobRet> queryWrapper = new QueryWrapper<>();

		 if(StringUtils.isNotBlank(netptJobRet.getDetectionType())){
			 queryWrapper.eq("netpt_detection.detection_type",netptJobRet.getDetectionType());
		 }
		 IPage<NetptJobRet> ipage = new Page<>(pageNo, pageSize);
		 IPage<NetptJobRet> pageList = netptJobRetService.findNetptJobRetList(ipage,queryWrapper);
		 return Result.OK(pageList);
	 }



	 /**
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "导出任务目标")
	 @ApiOperation(value="导出任务目标", notes="导出任务目标")
	 @GetMapping(value = "/exportJobTargetXls")
	 public ModelAndView exportJobTargetXls(HttpServletRequest request, @RequestParam(name="id",required=true) String id) {
		 NetptJob netptJob=netptJobService.getById(id);

		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 mv.addObject(NormalExcelConstants.FILE_NAME, "检测目标列表");
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("检测目标列表", "检测目标列表"));
		 if(netptJob.getDetectionTarget().equals("1")){
			 List<NetptJobIplist> netptJobIplistList = netptJobIplistService.selectByMainId(id);
			 mv.addObject(NormalExcelConstants.CLASS, NetptJobIplist.class);
			 mv.addObject(NormalExcelConstants.DATA_LIST, netptJobIplistList);
		 }else{
			 List<NetptJobSer> netptJobSerList = netptJobSerService.selectByMainId(id);
			 mv.addObject(NormalExcelConstants.CLASS, NetptJobSer.class);
			 mv.addObject(NormalExcelConstants.DATA_LIST, netptJobSerList);
		 }
		 return mv;
	 }

	 /**
	  * 导出任务检测结果excel
	  */
	 @AutoLog(value = "导出检测结果")
	 @ApiOperation(value="导出检测结果", notes="导出检测结果")
	 @GetMapping(value = "/exportALLJobRetXls")
	 public ModelAndView exportAllXls(HttpServletRequest request, @RequestParam(name="id") String id) {
		 QueryWrapper<NetptJobRet> queryWrapper = new QueryWrapper();
		 if(StringUtils.isNotBlank(id)){
			 queryWrapper.eq("netpt_job_ret.job_id",id);
		 }
		 List<NetptJobRet> queryList = netptJobRetService.findNetptJobRetList(queryWrapper);
		 // Step.4 AutoPoi 导出Excel
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 mv.addObject(NormalExcelConstants.FILE_NAME, "检测结果列表");
		 mv.addObject(NormalExcelConstants.CLASS, NetptJobRet.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("检测结果列表", "检测结果列表"));
		 mv.addObject(NormalExcelConstants.DATA_LIST, queryList);
		 return mv;
	 }
}
